package com.wave.blog.serve.service.impl;

import com.mybatisflex.spring.service.impl.ServiceImpl;
import com.wave.blog.serve.entity.Comment;
import com.wave.blog.serve.mapper.CommentMapper;
import com.wave.blog.serve.service.CommentService;
import org.springframework.stereotype.Service;
import java.time.LocalDateTime;
import java.util.List;

/**
 * 评论服务实现类
 * 处理评论相关的业务逻辑
 *
 * @author wave
 */
@Service
public class CommentServiceImpl extends ServiceImpl<CommentMapper, Comment> implements CommentService {

    @Override
    public List<Comment> getCommentsByArticleId(Long articleId) {
        // 使用MyBatis-Flex的条件查询，获取指定文章的已发布评论
        return queryChain().eq("article_id", articleId)
                .eq("status", 1)  // 只查询已发布状态的评论
                .orderBy("create_time", true)  // 按创建时间升序排列
                .list();
    }

    @Override
    public boolean addComment(Comment comment) {
        // 设置默认值
        comment.setCreateTime(LocalDateTime.now());
        comment.setUpdateTime(LocalDateTime.now());
        if (comment.getStatus() == null) {
            comment.setStatus(1);  // 默认已发布状态
        }
        if (comment.getParentId() == null) {
            comment.setParentId(0L);  // 默认顶级评论
        }
        return save(comment);
    }

    @Override
    public boolean deleteComment(Long id) {
        // 逻辑删除，更新状态为已删除
        return updateChain().set("status", 2)
                .set("update_time", LocalDateTime.now())
                .eq("id", id)
                .update();
    }
}